from db import DB


class Battle():
    def __init__(self, player, monster):
        self.__p = player # 玩家对象
        self.__m = monster # 怪物对象
        self.__battle_log = [] # 战斗日志
        self.__status = 0  # 战斗结果 0输 1赢

        self.monster_info = {
            "name": self.__m.name,
            "level": self.__m.level,
            "exp": self.__m.exp,
            "hp": self.__m.hp,
            "atk": self.__m.atk,
            "defense": self.__m.defense,
            "hit": self.__m.hit,
            "dod": self.__m.dod,
            "cri": self.__m.cri,
        }

        self.__run()

    def __run(self):
        """
        战斗流程
        """
        while True:
            attack = self.__attack()
            if attack: # 如果怪物死亡
                break

            be_attacked = self.__be_attacked()
            if be_attacked: # 如果玩家死亡
                break

    def __attack(self):
        """
        玩家攻击
        """
        p_a = self.__p.attack() # 玩家攻击
        m_b = self.__m.be_attacked(p_a[1], p_a[2]) # 怪物被攻击

        # start 战斗日志
        string = "你使出了<span style='color:orange'>[%s]</span>，" % p_a[0]
        if not m_b[0]:
            string += "却被 <span style='color:red'>[%s]</span> <span style='color:green'>躲避</span> 了" % self.__m.name
        else:
            string += "对 <span style='color:red'>[%s]</span> 造成了 <span style='color:green'>%d</span> 点 %s" % (self.__m.name, m_b[1], "<span style='red'>暴击</span>" if p_a[3] else "伤害")
        self.__battle_log.append(string)
        # end 战斗日志

        if self.__m.hp <= 0: # 判断怪物是否死亡
            self.__get_exp() # 怪物死亡获得经验值
            self.__status = 1  # 战斗结果为胜利
            return True
        return False

    def __be_attacked(self):
        """
        怪物攻击
        """
        m_a = self.__m.Mattack() # 怪物攻击
        p_b = self.__p.be_attacked(m_a[0], m_a[1]) # 玩家被攻击

        # start 战斗日志
        string = "<span style='color:red'>[%s]</span> 攻击了你，" % self.__m.name
        if not p_b[0]:
            string += "却被你 <span style='color:green'>躲避</span> 了~"
        else:
            string += "对你造成了 <span style='color:red'>%d</span> 点 %s" % (p_b[1], "<span style='color:red'>暴击</span>" if m_a[2] else "伤害")
        self.__battle_log.append(string)
        # end 战斗日志

        if self.__p.hp <= 0: #判断玩家是否死亡
            self.__battle_log.append("游戏结束！")
            return True

        return False

    def __get_exp(self):
        """
        怪物死亡流程
        """

        # 怪物死亡写入日志
        self.__battle_log.append("<span style='color:red'>[" + self.__m.name + "]</span> 被 <span style='color:green'>打败</span> 了！")

        exp = self.__m.exp # 怪物的经验值
        get_exp = self.__p.get_exp(exp, 0, []) # 玩家获得怪物经验值

        # 获得经验值写入日志
        self.__battle_log.append("你获得了 <span style='color:orange'>%d</span> 点经验值" % exp)
        self.__battle_log.append("战斗结束！")

        # 判断是否升级，升级信息写入日志
        if get_exp[0] > 0:
            if get_exp[0] == 1:
                self.__battle_log.append("恭喜<b>升级</b>！")
            else:
                self.__battle_log.append("恭喜连<b>升 <span style='color:green'>%d</span></b> 级！" % get_exp[0])

        if get_exp[1]:
            skill = DB().get_skill(",".join([str(i) for i in get_exp[1]]))
            for i in skill:
                self.__battle_log.append("学会了新技能 <span style='color:orange'>[" + i["name"] + "]</span>")
                
    def r(self):
        """
        返回战斗结果、战斗日志、玩家状态
        """
        battle_info = {
            "status": self.__status,
            "battle_log": self.__battle_log,
            "player": {
                "level": self.__p.level,
                "exp": self.__p.exp,
                "max_exp": self.__p.max_exp,
                "hp": self.__p.hp,
                "atk": self.__p.atk,
                "defense": self.__p.defense,
                "hit": self.__p.hit,
                "dod": self.__p.dod,
                "cri": self.__p.cri,
                "skill": self.__p.skill,
                "equipment": self.__p.equipment,
            },
            "monster": self.monster_info
        }

        return battle_info
